<template>
  <div class="optimize-container">
    <el-row>
      <el-col :span="24">
        <div style="text-align: right; padding-bottom: 5px;">
        <el-select v-model="curProject" placeholder="请选择" @change="changeProject">
          <el-option
            v-for="item in projects"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <el-select v-model="curCube" placeholder="请选择">
          <el-option
            v-for="item in cubes"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        </div>
      </el-col>
      <el-col :span="24">
        <div class="chart-wrapper">
          <div style="margin-left: 10px">
            <el-row>
              <el-col :span="8">
                <div class="chart-header">
                  <span>诊断评分</span>
                </div>
                <div>
                  <span style="float: left; width: 50px">总分</span>
                  <el-rate
                    v-model="scoreTotal"
                    disabled
                    show-score
                    text-color="#ff9900"
                    score-template="{value}">
                  </el-rate>
                </div>
                <div style="width:100%; padding: 12px 20px;" v-for="item in scoreData.indicator">
                    <div class="score-label">{{item.name}}</div>
                    <div class="score-value">{{item.value}}</div>
                    <div class="score-progress">
                      <el-progress :percentage="item.percentage" :show-text="false"></el-progress>
                    </div>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="chart-header">
                </div>
                <radar-chart :chart-data="scoreData" height="250px"></radar-chart>
              </el-col>
              <el-col :span="8" style="height: 100%">
                <div style="border-left: 2px solid rgb(185, 177, 177); height: 272px; padding: 0px 0px 0px 20px; ">
                  <el-tabs v-model="activeName">
                    <el-tab-pane label="评分解析" name="score">
                      <div style="overflow-y: auto; height: 220px;">
                        <div style="width:100%; padding-top: 8px;" v-for="(sugg, index) in scoreSuggests">
                          <div class="suggest-index">{{index+1}}.</div>
                          <div class="suggest-text">{{sugg}}</div>
                        </div>
                      </div>
                    </el-tab-pane>
                    <el-tab-pane label="Cube详情" name="info">
                      <div style="overflow-y: auto; height: 220px;">
                        <div style="width:100%; padding-top: 8px;" v-for="info in cubeInfo">
                          <div class="cube-name">{{info.name}}</div>
                          <div class="cube-value">：{{info.value}}</div>
                        </div>
                      </div>
                    </el-tab-pane>
                  </el-tabs>
                </div>
              </el-col>
            </el-row>
          </div>
        </div>
      </el-col>
      <el-col :span="24">
        <div class="chart-wrapper">

        </div>
      </el-col>
      <el-col :span="24">
        <div class="chart-wrapper">

        </div>
      </el-col>
      <el-col :span="24">
        <div class="chart-wrapper">

        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import RadarChart from './components/RadarChart'
  import BarChart from './components/BarChart'
  import optimizeApi from '@/api/kylin/optimize'
  import kylinUtil from '@/utils/kylinUtil'
  import commonApi from '@/api/kylin/common'

  export default {
    name: 'optimize',
    components: {
      RadarChart,
      BarChart
    },
    data() {
      return {
        projects: [],
        cubes: [],
        curProject: null,
        curCube: null,
        scoreData: {},
        scoreTotal: null,
        scoreSuggests: [],
        activeName: 'score',
        cubeInfo: []
      }
    },
    computed: {
      scoreProgress() {
        if (this.scoreData && this.scoreData.valueBeans.length > 0) {
          var dataValue = this.scoreData.valueBeans[0].data
          var indicator = this.scoreData.indicator
          indicator.map((item, index) => {
            item.value = dataValue[index]
            item.percentage = (item.value / item.max) * 100
          })
          return indicator
        }
      }
    },
    watch: {

      curCube: {
        deep: true,
        handler(val) {
          this.loadData()
        }
      }
    },
    mounted: function() {
      this.lastCube()
      this.loadProjects()
    },
    methods: {
      lastCube(cubeId) {
        optimizeApi.lastCube(cubeId).then(resp => {
          this.curProject = resp.data.projectId
          this.curCube = resp.data.cubeId
          this.loadCubes(false)
        })
      },
      loadData() {
        this.score()
        this.cube()
      },
      loadProjects() {
        commonApi.projectsExAll().then(resp => {
          if (resp.data) {
            this.projects = resp.data
          }
        })
      },
      loadCubes(tag) {
        commonApi.cubesExAll(this.curProject).then(resp => {
          if (resp.data && resp.data.length > 0) {
            this.cubes = resp.data
            if (tag) {
              this.curCube = resp.data[0].value
            }
          }
        })
      },
      score() {
        optimizeApi.score(this.curCube).then(resp => {
          if (resp.data) {
            resp.data.indicator.map((item, index) => {
              item.value = resp.data.valueBeans[0].data[index]
              item.percentage = (item.value / item.max) * 100
            })

            this.scoreData = resp.data
            this.scoreTotal = resp.data.valueBeans[0].avg
            this.scoreSuggests = resp.data.suggests
            if (this.scoreSuggests.length === 0) {
              this.scoreSuggests.push('Cube评分良好')
            }
          }
        })
      },
      cube() {
        optimizeApi.cube(this.curCube).then(resp => {
          this.cubeInfo = []
          if (resp.data) {
            var item = resp.data
            this.cubeInfo.push({ name: '名称', value: item.name })
            this.cubeInfo.push({ name: '状态', value: item.status })
            this.cubeInfo.push({ name: '创建时间', value: kylinUtil.formatDate(item.createTime) })
            this.cubeInfo.push({ name: '膨胀倍数', value: Number(item.swell).toFixed(2) })
            this.cubeInfo.push({ name: '查询', value: item.queryCount })
            this.cubeInfo.push({ name: '慢查询', value: item.slowQueryCount })
            this.cubeInfo.push({ name: 'Cubeids', value: item.cuboids })
          }
        })
      },
      changeProject() {
        this.loadCubes(true)
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  body {
    font-size: 12px;
  }
  .cube-name {
    text-align: justify;
    text-align-last: justify;
    float: left;
    width: 70px;
  }
  .cube-value {
    margin-left: 70px;
  }
  .score-label {
    float: left;
    width: 80px;
    line-height: 30px;
  }
  .score-value {
    float: right;
    width: 20px;
    line-height: 30px;
  }
  .score-progress {
    margin: 0px 30px 0px 85px;
    line-height: 30px;
    padding-top: 12px;
  }
  .suggest-index {
    float: left;
    width: 20px;
  }
  .suggest-text {
    margin: 0px 5px 0px 20px;
    word-break: break-all;
  }
  .optimize-container {
    padding: 15px;
    background-color: rgb(240, 242, 245);

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px;
    margin-bottom: 32px;
    border-radius: 8px;

  .chart-header {
    margin-bottom: 15px;
    line-height: 28px;
    display: flex;
    justify-content: space-between;

  i {
    margin-left: 5px;
    margin-right: 5px;
    cursor: pointer;

  }

  .active {
    color: #409EFF;
  }

  .el-date-editor--daterange {
    width: 230px;

  }

  }
  }
  }

</style>
